package com.zc.custom.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zc.custom.domain.ChargeCustomOrderInfoDetails;
import com.zc.util.ArithmeticUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.zc.custom.domain.ChargeCustomOrderInfo;
import com.zc.custom.service.IChargeCustomOrderInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户订单Controller
 *
 * @author glb
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/custom/orderInfo")
public class ChargeCustomOrderInfoController extends BaseController
{
    @Autowired
    private IChargeCustomOrderInfoService chargeCustomOrderInfoService;

    /**
     * 查询用户订单列表
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChargeCustomOrderInfo chargeCustomOrderInfo)
    {
        startPage();
        List<ChargeCustomOrderInfo> list = chargeCustomOrderInfoService.selectChargeCustomOrderInfoList(chargeCustomOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出用户订单列表
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:export')")
    @Log(title = "用户订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChargeCustomOrderInfo chargeCustomOrderInfo)
    {
        List<ChargeCustomOrderInfo> list = chargeCustomOrderInfoService.selectChargeCustomOrderInfoList(chargeCustomOrderInfo);
        for (ChargeCustomOrderInfo customOrderInfo : list) {
            customOrderInfo.setHourTime(String.valueOf(ArithmeticUtils.div(customOrderInfo.getTotalTime(),3600)));
        }
        ExcelUtil<ChargeCustomOrderInfo> util = new ExcelUtil<>(ChargeCustomOrderInfo.class);
        util.exportExcel(response, list, "用户订单数据");
    }

    /**
     * 获取用户订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:query')")
    @GetMapping(value = "/{orderInfoId}")
    public AjaxResult getInfo(@PathVariable("orderInfoId") Long orderInfoId)
    {
        return AjaxResult.success(chargeCustomOrderInfoService.selectChargeCustomOrderInfoByOrderInfoId(orderInfoId));
    }

    /**
     * 获取用户订单统计信息
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:getInfoCount')")
    @GetMapping(value = "/getInfoCount")
    public AjaxResult getInfoCount()
    {
        return AjaxResult.success(chargeCustomOrderInfoService.getInfoCount());
    }



    /**
     * 新增用户订单
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:add')")
    @Log(title = "用户订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChargeCustomOrderInfo chargeCustomOrderInfo)
    {
        return toAjax(chargeCustomOrderInfoService.insertChargeCustomOrderInfo(chargeCustomOrderInfo));
    }

    /**
     * 修改用户订单
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:edit')")
    @Log(title = "用户订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChargeCustomOrderInfo chargeCustomOrderInfo)
    {
        return toAjax(chargeCustomOrderInfoService.updateChargeCustomOrderInfo(chargeCustomOrderInfo));
    }

    /**
     * 删除用户订单
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:remove')")
    @Log(title = "用户订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderInfoIds}")
    public AjaxResult remove(@PathVariable Long[] orderInfoIds)
    {
        return toAjax(chargeCustomOrderInfoService.deleteChargeCustomOrderInfoByOrderInfoIds(orderInfoIds));
    }

    /**
     * 查询设备充电记录
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:list')")
    @GetMapping("/orderList")
    public AjaxResult getOrderList(String deviceId, String portId, @RequestParam Date startDate, @RequestParam Date endDate){
        if(deviceId == null && portId == null){
            throw new RuntimeException("参数不全");
        }
        return AjaxResult.success(chargeCustomOrderInfoService.getOrderList(deviceId, portId, startDate, endDate));
    }

    /**
     * 查询用户订单详情
     */
    @PreAuthorize("@ss.hasPermi('custom:orderInfo:orderInfoDetailsList')")
    @GetMapping("/getOrderInfoDetailsList")
    public TableDataInfo getOrderInfoDetailsList(ChargeCustomOrderInfo chargeCustomOrderInfo)
    {
        List<ChargeCustomOrderInfoDetails> list = chargeCustomOrderInfoService.getOrderInfoDetailsList(chargeCustomOrderInfo);
        return getDataTable(list);
    }
}
